#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : BaseEditor(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    table=REF_CPU;
    listModel=new QStringListModel(this);
    ui->listView->setModel(listModel);

    model=new QSqlTableModel(this);
    ui->tableView->setModel(model);

    createMenu();
    updateFamily();
    searchBox=nullptr;
}

Widget::~Widget()
{
    delete ui;
}

void Widget::generateMenu()
{
    menu->addAction("搜索... (F1)",this,&Widget::search);
    menu->addAction("添加... (F2)",this,&Widget::newItem);
    menu->addSeparator();
    menu->addAction("更新列表 (F4) ",this,&Widget::updateFamily);
    menu->addAction("更新视图 (F5) ",this,&Widget::updateView);
    menu->addSeparator();
    QMenu *m_opt=menu->addMenu("选项");
    m_opt->addAction("撤销 (Ctrl+Z)",this,&Widget::revert);
    m_opt->addAction("保存 (Ctrl+S)",this,&Widget::save);
    m_opt->addSeparator();
    m_opt->addAction("删除 (Delete)",this,&Widget::removeItem);
}

void Widget::newItem()
{
    AddItem item(this);
    item.exec();
}

void Widget::removeItem()
{
    QModelIndex index=ui->tableView->currentIndex();
    if(index.isValid())
        model->removeRow(index.row());
}

void Widget::updateFamily()
{
    QStringList items=sp_getCategory("FirstName",table);
    listModel->setStringList(items);
}

void Widget::keyPressEvent(QKeyEvent *event)
{
    if(event->modifiers() == Qt::ControlModifier && event->key() == Qt::Key_S)
        save();
    else if(event->modifiers() == Qt::ControlModifier && event->key() == Qt::Key_Z)
        revert();

    switch (event->key()) {
    case Qt::Key_F1:
        search();
        break;
    case Qt::Key_F2:
        newItem();
        break;
    case Qt::Key_F4:
        updateFamily();
        break;
    case Qt::Key_F5:
        updateView();
        break;
    case Qt::Key_Delete:
        removeItem();
        break;
    }
}

void Widget::on_listView_doubleClicked(const QModelIndex &index)
{
    if(index.isValid())
    {
        selectFamily=index.data().toString();
        updateView();
    }
}

void Widget::updateView()
{
    if(selectFamily.isEmpty())
        return;

    workForm=WF_NORMAL;
    model->setTable(table);
    model->setFilter(QString("FirstName='%1'").arg(selectFamily));
    model->select();
    setHeaderData();
}

void Widget::search()
{
    if(searchBox == nullptr)
    {
        searchBox=new SearchBox(this);
        connect(searchBox,&SearchBox::request,this,&Widget::onRequest);
    }

    searchBox->show();
}

void Widget::onRequest(int flag, const QString &kw)
{
    workForm=WF_SEARCH;

    QString url;
    switch (flag) {
    case SF_NONE:
    {
        model->setTable(table);
        model->setFilter(QString("LastName like '%%1%'").arg(kw));
        model->select();
        if(model->rowCount() > 0)
        {
            setHeaderData();
            return;
        }
        else
            url=BAIDU_PREFIX + kw;
    }
        break;
    case SF_AMD:
        url=AMD_QUERY + kw;
        break;
    case SF_INTEL:
        url=INTEL_QUERY + kw;
        break;
    }

    QDesktopServices::openUrl(QUrl(url));
}

void Widget::setHeaderData()
{
    for(int i=0;i < headerColumns.size();i++)
    {
        model->setHeaderData(i,Qt::Horizontal,headerColumns.value(i));
    }

    ui->tableView->hideColumn(0);   //ID
    ui->tableView->resizeColumnsToContents();
}
